package com.mymall.serve.controller;

import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.mymall.serve.common.RootResult;
import com.mymall.serve.dto.param.RoleUserParam;
import com.mymall.serve.dto.param.UserAddParam;
import com.mymall.serve.service.MallUserService;
import com.mymall.serve.service.SysRoleUserLinkService;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.validation.Valid;
import javax.xml.xpath.XPathConstants;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 用户操作类
 *
 * @author weiwei
 * @since 2020-04-12 11:45
 **/
@Validated
@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private MallUserService userService;

    @Autowired
    private SysRoleUserLinkService roleUserLinkService;

    @ApiOperation(value = "新增用户及角色 ", notes = "新增用户及角色")
    @PostMapping(value = "/addUser")
    public RootResult<String> addUser(@RequestBody @Valid UserAddParam sysUserAddParam) {
//        Long createrId = RequestThreadLocal.getUserId();

        Long userId = userService.insertUser(sysUserAddParam, 1L);
        List<Long> userIds = new ArrayList<>();
        userIds.add(userId);
        RoleUserParam rParam = new RoleUserParam();
        rParam.setUserIds(userIds);
        rParam.setRoleIds(sysUserAddParam.getRoleIds());
        roleUserLinkService.addRoleToUser(rParam);
        return RootResult.success();
    }

}
